<template>

  <div class="app-container calendar-list-container">

    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="名字" v-model="listQuery.name">
      </el-input>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">新增</el-button>
      <!--<el-button class="filter-item" type="primary" :loading="downloadLoading"  icon="el-icon-download" @click="handleDownload">导出</el-button>-->
    </div>

    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row >
      <el-table-column align="center" label='ID' width="100">
        <template slot-scope="scope">
          {{scope.$index}}
        </template>
      </el-table-column>
      <el-table-column label="等级ID" width="100" align="center">
        <template slot-scope="scope">
          {{scope.row.levelId}}
        </template>
      </el-table-column>
      <el-table-column label="等级名称" width="300" align="center">
        <template slot-scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="开始阈值" width="160" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.number}}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束阈值" width="160" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.endNumber}}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" width="120" align="center">
        <template slot-scope="scope">
          {{scope.row.sort}}
        </template>
      </el-table-column>


      <el-table-column  label="操作"  align="center"  >
        <template slot-scope="scope">
          <el-button type="primary" @click="handleUpdate(scope.row)" size="mini">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="80px" style='width: 400px; margin-left:50px;'>


        <el-form-item label="名称" prop="name">
          <el-input v-model="temp.name" placeholder="等级名称"></el-input>
        </el-form-item>

        <el-form-item label="开始阈值" prop="number">
          <el-input v-model="temp.number" placeholder="开始值"></el-input>
        </el-form-item>

        <el-form-item label="结束阈值" prop="endNumber">
          <el-input v-model="temp.endNumber" placeholder="结束值"></el-input>
        </el-form-item>

        <el-form-item label="排行" prop="sort">
          <el-input v-model="temp.sort"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  @click="dialogFormVisible = false">取消</el-button>
        <el-button :loading="buttonLoading" v-if="dialogStatus=='create'" type="primary" @click="createData">确认</el-button>
        <el-button  :loading="buttonLoading" v-else type="primary" @click="updateData">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>

  .filter-container{
    padding-bottom: 20px;
  }

</style>

<script>
import { List, Merge } from '../../api/level'

export default {

  data() {
    return {
      list: null,
      listLoading: true,
      buttonLoading: false,
      listQuery: {
        name: null
      },
      temp: {
        levelId: null,
        name: '',
        number: '',
        endNumber: '',
        sort: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      downloadLoading: false,
      textMap: {
        update: '编辑等级信息：',
        create: '新增等级信息：'
      },
      rules: {
        name: [{ required: true }],
        number: [{ required: true }],
        endNumber: [{ required: true }],
        sort: [{ required: true }]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      List(0, 1, this.listQuery.name, 'sort asc').then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    handleFilter() {
      this.fetchData()
    },
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.resetTemp()
      this.temp = Object.assign(this.temp, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    handleDownload() {

    },
    createData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          Merge(this.temp).then(res => {
            this.temp = res.data
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.buttonLoading = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            this.fetchData()
          }).catch(reason => {
            console.log(reason)
            this.buttonLoading = false
          })
        } else {
          console.log('验证失败')
        }
      })
    },
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          Merge(this.temp).then(res => {
            for (const v of this.list) {
              if (this.temp.levelId === v.levelId) {
                const index = this.list.indexOf(v)
                console.log(index)
                this.list.splice(index, 1, this.temp)
                break
              }
            }
            this.dialogFormVisible = false
            this.buttonLoading = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          }).catch(reason => {
            console.log(reason)
            this.buttonLoading = false
          })
        }
      })
    },
    resetTemp() {
      this.temp = {
        levelId: null,
        name: '',
        number: '',
        endNumber: '',
        sort: ''
      }
    }
  }
}
</script>
